package top.fzqblog.service;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;
import top.fzqblog.Utils.R;
import top.fzqblog.model.CrawlerUser;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 抽离 on 2017/7/17 0017.
 */
@Service
public class CrawlerUserService {

    private AtomicInteger UKCount = new AtomicInteger(0);


    public void crawler(){
        String url = "http://www.bdyunso.com/user-drshare-daren/";
        try {
            Document document =
                        Jsoup
                        .connect(url)
                        .timeout(5000)
                        .get();
            Elements elements = document.select(".c_l a");
            for (Element element : elements){
                crawlerPanUser("http://www.bdyunso.com/" + element.attr("href").replaceAll("/", ""));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public void crawlerPanUser(String url){
        try {
            Document document = Jsoup
                    .connect(url)
                    .timeout(5000)
                    .get();
            Optional.ofNullable(document.select(".pages small"))
                    .ifPresent(
                            (pageEle) ->{
                                String pageReg = "(\\d+)/(\\d+)";
                                Pattern pattern = Pattern.compile(pageReg);
                                Matcher matcher = pattern.matcher(pageEle.text().trim());
                                if(matcher.find()){
                                    int pages = Integer.parseInt(matcher.group(2));
                                    System.out.println("共有" + pages + "页");
                                    ExecutorService executorService = Executors.newFixedThreadPool(10);
                                    Future<Map> future = null;
                                    for(int i = 1; i <= pages; i++){
                                         future = executorService.submit(
                                                new CrawlerUserThread(url +"-" + i)
                                        );
                                    }
                                    while (future != null && !future.isDone()){
                                        try {
                                            Thread.sleep(3000);
                                            System.out.println("结果：" + future.get());
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        } catch (ExecutionException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                    System.out.println("总共有：" + UKCount + "个");
                                }
                            }
                    );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    class CrawlerUserThread implements Callable<Map>{
        private String url;

        CrawlerUserThread(String url) {
            this.url = url;
        }

        @Override
        public Map<String, Object> call() throws Exception {
            try {
                System.out.println("url：" + url);
                Document document = Jsoup
                        .connect(url)
                        .timeout(5000)
                        .get();
                Elements elements = document.select(".user_info");
                for (Element element : elements){
                    CrawlerUser crawlerUser = new CrawlerUser();
                    String href = element.select(".face").attr("href");
                    String avatarUrl = element.select("img").attr("src");
                    String name = element.select(".user-name").text();
                    Pattern pattern = Pattern.compile("\\d+");
                    Matcher matcher = pattern.matcher(href);
                    if(matcher.find()){
                        crawlerUser.setUk(matcher.group(0));
                    }
                    crawlerUser.setName(name);
                    crawlerUser.setAvatarUrl(avatarUrl);
                    try {
                        crawlerUser.insert();
                        UKCount.incrementAndGet();
                    }catch (Exception e){
                        Throwable cause = null;
                        if(((cause = e.getCause())!=null
                                && (cause = cause.getCause())!=null
                                && (cause = cause.getCause())!=null
                                && cause.getClass().getName().equals("com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException")
                                && cause.getMessage().startsWith("Duplicate entry"))){
                           System.out.println("出现重复记录");
                        }
//                        System.out.println("：" + e.getCause().getCause().getCause().getMessage());
//                            e.printStackTrace();
                    }
                }
                return R.ok();
            } catch (IOException e) {
                e.printStackTrace();
                return R.error();
            }
        }
    }


}
